package com.kolon.business.mOI.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mOI.controller.MOIIdeaSuggestionViewController")
public class MOIIdeaSuggestionViewController implements SimpleController {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/business/mOI/mOIIdeaSuggestionView.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		// XML URL
		String url = MsgUtil.getMessage("xml.url");
		// 파라미터
		String seq = request.getParameter("seq");
		String sugflag = ComUtil.nullChk(request.getParameter("sugflag")).equals("A00078") ? "pubSubj":"";
		String page = request.getParameter("page");
		// 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String userId = (String)userMap.get("userId");
		String userPw = (String)userMap.get("userPw");
		String cpyCD = (String)userMap.get("CompanyCD");
		// URI
		StringBuffer buffer = new StringBuffer();
		buffer.append(url); 
		buffer.append("/oi_suggestion.aspx?strGubun=ideaView&mode=1&PageFlag=e2");
		buffer.append("&Seq=").append(seq);
		buffer.append("&companycd=").append(cpyCD);
		buffer.append("&pType=").append(sugflag);
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("BoardDetail");
		Iterator itXml = xmlList.iterator();
		Map map = null;
		// XML 데이터
		if (itXml.hasNext()) {
			map = new HashMap();
			Element el = (Element)itXml.next();
			map.put("SugID", el.getChild("SugID").getText());
			map.put("EmpID", el.getChild("EmpID").getText());
			map.put("EmpName", el.getChild("EmpName").getText());
			map.put("CompanyCD", el.getChild("CompanyCD").getText());
			map.put("Title", el.getChild("Title").getText());
			map.put("Content", el.getChild("Content").getText().replaceAll("&lt;","<").replaceAll("&gt;",">").replaceAll("&nbsp;"," ").replaceAll("&amp;","&"));
			map.put("CreateDate", el.getChild("CreateDate").getText());
			map.put("KeepFlag", el.getChild("KeepFlag").getText());
			map.put("SugFlag", el.getChild("SugFlag").getText());
			map.put("ACCEPTDEPTNAME", el.getChild("ACCEPTDEPTNAME").getText());
			map.put("DeptName", el.getChild("DeptName").getText());
			map.put("TelNum", el.getChild("TelNum").getText());
			map.put("StatusCD", el.getChild("StatusCD").getText());
			map.put("TitleName", el.getChild("TitleName").getText());
			map.put("pubSubj", el.getChild("pubSubj").getText());
			map.put("Unsigned", el.getChild("Unsigned").getText());
		}
		element = doc.getRootElement();
		xmlList = element.getChildren("Board2");
		itXml = xmlList.iterator();
		// XML 데이터
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map.put("JUDGEDATE", el.getChild("JUDGEDATE").getText());
			map.put("JUDGEOPINION", el.getChild("JUDGEOPINION").getText());
		}
		// ---[심사자 정보]-------------------------------------------------------
		element = doc.getRootElement();
		xmlList = element.getChildren("Board6");
		itXml = xmlList.iterator();
		// XML 데이터
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map.put("JUDGEEMPNAME", el.getChild("JUDGEEMPNAME").getText());
			map.put("JUDGEEMPTITLE", el.getChild("JUDGEEMPTITLE").getText());
		}
		// ---[심사결과 정보]-----------------------------------------------------
		map.put("JUDGERESULT", "대기");
		element = doc.getRootElement();
		xmlList = element.getChildren("Board2");
		itXml = xmlList.iterator();
		// XML 데이터
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map.put("JUDGERESULT", el.getChild("JUDGERESULT").getText());
		}
		// ---[심사부서 변경횟수]--------------------------------------------------
		element = doc.getRootElement();
		xmlList = element.getChildren("Board4");
		itXml = xmlList.iterator();
		// XML 데이터
		if (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			if (el.getChild("DEPTNUM") != null)
				map.put("DEPTNUM", el.getChild("DEPTNUM").getText());
		}
		// ---[심사부서 변경횟수]--------------------------------------------------
		element = doc.getRootElement();
		xmlList = element.getChildren("Board3");
		itXml = xmlList.iterator();
		String judgeDept = "";
		// XML 데이터
		while (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			judgeDept += el.getChild("JUDGEDEPTCD").getText() + "##";
		}
		String[] judgeDetpArr = judgeDept.split("##");
		judgeDept = "";
		for (int i=0; i<judgeDetpArr.length; i++) {
			if (i != judgeDetpArr.length-1)
				judgeDept += judgeDetpArr[i] + " &gt; ";
			else
				judgeDept += judgeDetpArr[i];
		}
		map.put("judgeDept", judgeDept);
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("map", map);
		modelAndView.addObject("page", page);
		String edtChk = "N";
		// 글내용 중 태그 존재시 (PC에서 에디터를 활용해서 등록한 경우 체크)
		if (((String)map.get("Content")).indexOf("<html>") != -1) {
			edtChk = "Y";
		}
		modelAndView.addObject("edtChk", edtChk);
		return modelAndView;
	}

}
